package m.jclecai.com.fragment.news;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.activity.WebHelperActivity;
import m.jclecai.com.adapter.base.ViewHolerAdapter;
import m.jclecai.com.adapter.holder.ViewHolder;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.bean.Response.ResNewsList;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by andy on 2017/7/21.
 * <p>
 * 新闻的基础的父类
 */

public class NewsFragment extends BaseFragment {

    @BindView(R.id.prl_list_view)
    PullToRefreshListView mPrlListView;

    private int mPosition = 0;
    List<ResNewsList.NewsBean> list=new ArrayList<>();
    private MyAdapter mMyAdapter;

    public void setPosition(int position) {
        mPosition = position;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_news;
    }

    @Override
    public void initData() {
        super.initData();
        mMyAdapter = new MyAdapter(list);
        mPrlListView.setAdapter(mMyAdapter);

        mPrlListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String url= "http://m.yuecp.net/view/index.html?id="+mMyAdapter.getNewsBeanList().get(position-1).getId();
                startActivity(WebHelperActivity.getInstance(getActivity(),url,false,"新闻详情"));
            }
        });

    }

    /**
     * 请求数据；
     */
    public void requestData(String type) {
        //newsList.php
        HashMap<String, String> params = new HashMap<>();
        params.put("type",type);
        HttpManager.postAsyn(Constant_Url.NEWS_LIST, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                DisPlay("网络异常！！！");
            }

            @Override
            public void onResponse(String response) {

                if(!TextUtils.isEmpty(response)){

                    ResNewsList resNewsList = FastOk.get(response, ResNewsList.class);
                    if ("0".equals(resNewsList.getStatus())) {

                        setData(resNewsList.getRows());
                    }else{
                        DisPlay(resNewsList.getMessage());
                    }

                }else{
                    //这里表示返回为空；
                    DisPlay("网络异常！！！");
                }

            }
        },params);
    }

    public void setData(List<ResNewsList.NewsBean> bean) {

        list.addAll(bean);
        mMyAdapter.notifyDataSetChanged();
    }


    public class MyAdapter extends ViewHolerAdapter<NewsViewHolder> {

        public MyAdapter(List<ResNewsList.NewsBean> newsBeanList) {
            mNewsBeanList = newsBeanList;
        }

        List<ResNewsList.NewsBean> mNewsBeanList;


        @Override
        public NewsViewHolder onCreateViewHolder(int position, ViewGroup parent) {
            return new NewsViewHolder(getContext(), R.layout.item_list_news);
        }

        @Override
        public void onBindViewHolder(int position, NewsViewHolder myViewHolder) {
            ResNewsList.NewsBean newsBean = mNewsBeanList.get(position);

            Glide.with(getContext()).load(newsBean.getFilename()).into(myViewHolder.mIvNewsLogo);
            myViewHolder.mTvContent.setText(newsBean.getTitle());
            myViewHolder.mTvTitle.setText(newsBean.getTitle());
            myViewHolder.mTvDate.setText(newsBean.getTime());

            myViewHolder.mTvIsStart.setVisibility(View.INVISIBLE);
        }

        @Override
        public int getCount() {
            return mNewsBeanList.size();
        }

        public List<ResNewsList.NewsBean> getNewsBeanList(){
            return mNewsBeanList;
        }

    }

    public class NewsViewHolder extends ViewHolder {

        @BindView(R.id.iv_news_logo)
        ImageView mIvNewsLogo;
        @BindView(R.id.tv_title)
        TextView mTvTitle;
        @BindView(R.id.tv_is_start)
        TextView mTvIsStart;
        @BindView(R.id.tv_date)
        TextView mTvDate;
        @BindView(R.id.tv_content)
        TextView mTvContent;

        public NewsViewHolder(Context context, @LayoutRes int resource) {
            super(context, resource);
        }
    }


}
